package com.fenquen.dfs.base.protocol;

import com.alibaba.fastjson.annotation.JSONField;
import com.fenquen.dfs.base.protocol.data.Data;

import java.nio.ByteBuffer;

public class Protocol {


    /**
     * 协议种类,1个字节
     */
    public Byte protocolType;
    public static final int protocolTypeByteLength = 1;

    public interface ProtocolType {
        interface ReqFromStorage {
            byte 存储节点自身状态信息 = 0x01;
            byte 存储节点文件上传回执 = 0x02;
        }
    }

    /**
     * 内容长度,2个字节
     */
    public Short dataLength;
    public static final int dataLengthByteLength = 2;


    public Data data;

    @JSONField(serialize = false)
    public byte[] getByteArr() {
        ByteBuffer byteBuffer = ByteBuffer.allocate(protocolTypeByteLength +
                dataLengthByteLength + data.getByteLength());

        byteBuffer.put(protocolType);
        byteBuffer.putShort(dataLength);
        byteBuffer.put(data.getByteArr());

        return byteBuffer.array();
    }
}
